<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        let treeData = [
            {
                node: 2, children: [3, 9, 4]
            }
            , {
                node: 7, children: [2]
            },
            {
                node: 3, children: [6]
            },
            {
                node: 4, children: [5]
            },
            {
                node: 5, children: [8]
            },
            {
                node: 10, children: [11]
            }
        ]
        function getChildren(tree = [], parent = [], result) {
            let children = []
            if (parent.length > 0) {
                for (let i = 0; i < parent.length; i++) {
                    let nodeData = tree.find(item => item.node == parent[i]);
                    if (nodeData) {
                        children.push(...nodeData.children);
                    }
                }
                result.push(...parent);
                getChildren(tree, children, result);
            }
        }
        function buildArray(tree = []) {
            let result = [];
            let parent = []
            for (let i = 0; i < tree.length; i++) {
                let node = tree[i].node;
                let node_parent = tree.find(item => item.children.includes(node))
                if (!node_parent) {
                    parent.push(node);
                }
            }
            getChildren(tree, parent, result);
            return result;
        }
        let result = buildArray(treeData);
        console.log(result);
    </script>
</body>

</html>